package cn.oms.manage.service.impl;

import cn.oms.commons.ex.ServiceException;
import cn.oms.commons.pojo.vo.PageData;
import cn.oms.commons.util.PageInfoToPageDataConverter;
import cn.oms.commons.util.StringToDate;
import cn.oms.commons.web.ServiceCode;
import cn.oms.manage.dao.persist.repository.IOrderRepositry;
import cn.oms.manage.pojo.entity.Order;
import cn.oms.manage.pojo.param.OrderAddParam;
import cn.oms.manage.pojo.param.OrderUpdateParam;
import cn.oms.manage.pojo.vo.OrderListVo;
import cn.oms.manage.pojo.vo.OrderStandardVO;
import cn.oms.manage.service.IOrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Random;

@Service
@Slf4j
public class OrderServiceImpl implements IOrderService {
    @Autowired
    IOrderRepositry orderRepositry;

    @Override
    public OrderStandardVO getStandardById(Long id) {
        return orderRepositry.getStandardById(id);
    }

    @Override
    public List<OrderListVo> listByUsername(String username) {
        return orderRepositry.listByUsername(username);
    }

    @Override
    public List<OrderListVo> listByDoctor(String doctorName) {
        return orderRepositry.listByDoctor(doctorName);
    }

    @Override
    public List<OrderListVo> listByState(String state) {
        return orderRepositry.listByState(state);
    }

    @Override
    public void insert(OrderAddParam orderAddParam) {
        log.debug("开始处理【添加订单】的业务，参数：{}", orderAddParam);

        Order order = new Order();
        BeanUtils.copyProperties(orderAddParam, order);
        order.setAppointment(StringToDate.StrToDate(orderAddParam.getAppointment()));
        order.setState("1");
//        Long customerId = orderRepositry.getCustomerId(orderAddParam.getPhoneNumber(),orderAddParam.getUsername());
//        if (customerId>0){
//            order.setCustomerId(customerId);
//        }else {
//            order.setCustomerId(new Random().nextLong());
//        }
        order.setCustomerId(new Random().nextLong()+1);

        order.setGmtCreateTime(LocalDateTime.now());

        log.debug("即将执行插入数据，参数：{}", order);
        int rows = orderRepositry.insert(order);
        if (rows != 1) {
            String message = "添加订单失败！服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    @Override
    public void deleteById(Long id) {
        orderRepositry.deleteById(id);
    }

    @Override
    public void updateById(Long id, OrderUpdateParam orderUpdateParam) {
        orderRepositry.updateById(id,orderUpdateParam);
    }

    //更新状态
    @Override
    public void updateState() {
        List<OrderListVo> list = orderRepositry.list();
        for (OrderListVo orderListVo : list) {
            if(orderListVo.getAppointment().isBefore(LocalDateTime.now())
                && orderListVo.getState().equals("1")){
                orderRepositry.updateState(orderListVo.getId());
            }
        }

    }

    @Override
    public PageData<OrderListVo> list(Integer pageNum) {
        log.debug("开始处理【查询药品列表】的业务，页码：{}", pageNum);
        PageData<OrderListVo> pageData = list(pageNum, 6);
        return pageData;
    }

    @Override
    public PageData<OrderListVo> list(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询药品列表】的业务，页码：{}，每页记录数：{}", pageNum, pageSize);
        List<OrderListVo> list = orderRepositry.list();
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<OrderListVo> pageInfo = new PageInfo<>(list);
        PageData<OrderListVo> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }
}
